/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   演示ATIM输出6步换向PWM, BK管脚低电平进行刹车，刹车解除后自动恢复
  *          CH1  = PB00, CH2  = PB01, CH3  = PA03
  *          CH1N = PA04, CH2N = PA05, CH3N = PA06
  *          BKIN = PB07(RSTB)
  *          将RESET复用成通用IO，配置复用功能为ATIM_BKIN, 按下按键(PB07/RSTB)进行刹车，松开按键解除刹车
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：None
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_atim.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/
#define OCMODE_PWM1        6
#define OCMODE_FORCED      5

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
ATIM_HandleTypeDef hatim;
volatile uint32_t  Step = 1;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void ATIM_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // 将RESET复用成普通IO
    HAL_SysCtrl_RESET2GPIO();

    // 初始化ATIM
    ATIM_Init();

    while (1)
    {
        ;  // 在中断服务程序中进行PWM调节，实现呼吸灯效果
    }
}


/*******************************************************************************
 * @brief  ATIM初始化
 * @param  None
 * @retval None
 */
static void ATIM_Init(void)
{
    GPIO_InitTypeDef                 GPIO_InitStruct;
    ATIM_BreakDeadTimeConfigTypeDef  ATIM_BDTCfgStruct;
    ATIM_BreakInputConfigTypeDef     ATIM_BKInCfgStruct;
    MemClr(&GPIO_InitStruct,    sizeof(GPIO_InitStruct));
    MemClr(&ATIM_BDTCfgStruct,  sizeof(ATIM_BDTCfgStruct));
    MemClr(&ATIM_BKInCfgStruct, sizeof(ATIM_BKInCfgStruct));

    HAL_SYSCTRL_GPIOA_CLK_ENABLE();
    HAL_SYSCTRL_GPIOB_CLK_ENABLE();
    HAL_SYSCTRL_ATIM_CLK_ENABLE();

    //------------------------------------------------------------
    // 配置输出端口 ATIM_CH1/2/3/1N/2N/3N
    AFx_ATIMCH1_PB00();
    AFx_ATIMCH2_PB01();
    AFx_ATIMCH3_PA03();
    AFx_ATIMCH1N_PA04();
    AFx_ATIMCH2N_PA05();
    AFx_ATIMCH3N_PA06();

    HAL_GPIO_WritePins(SYM_GPIOA, GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6, GPIO_PIN_RESET);
    GPIO_InitStruct.Pins = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    HAL_GPIO_WritePins(SYM_GPIOB, GPIO_PIN_0 | GPIO_PIN_1, GPIO_PIN_RESET);
    GPIO_InitStruct.Pins = GPIO_PIN_0 | GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    //------------------------------------------------------------
    // 配置BKIN管脚
    AFx_ATIMBK_PB07();
    GPIO_InitStruct.Pins = GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULL_UP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    //------------------------------------------------------------
    // 配置ATIM
    MemClr(&hatim, sizeof(hatim));

    hatim.Instance               = SYM_ATIM;
    hatim.Init.ClockDivision     = ATIM_CLOCKDIVISION_DIV1;
    hatim.Init.CounterMode       = ATIM_COUNTERMODE_UP;
    hatim.Init.Prescaler         = 48 - 1;
    hatim.Init.Period            = 4096 - 1;
    hatim.Init.RepetitionCounter = 0;
    hatim.Init.AutoReloadPreload = ATIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_ATIM_Base_Init(&hatim);

    hatim.OcInit.OCMode       = ATIM_OCMODE_TIMING;
    hatim.OcInit.OCPolarity   = ATIM_OCPOLARITY_HIGH;
    hatim.OcInit.OCNPolarity  = ATIM_OCNPOLARITY_HIGH;
    hatim.OcInit.OCFastMode   = ATIM_OCFAST_ENABLE;
    hatim.OcInit.OCIdleState  = ATIM_OCIDLESTATE_SET;
    hatim.OcInit.OCNIdleState = ATIM_OCNIDLESTATE_SET;
    hatim.OcInit.Pulse        = 2048 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_1);
    hatim.OcInit.Pulse        = 1024 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_2);
    hatim.OcInit.Pulse        = 512 - 1;
    HAL_ATIM_PWM_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_3);

    // 配置死区时间及刹车状态
    ATIM_BDTCfgStruct.AutomaticOutput  = ATIM_AUTOMATICOUTPUT_ENABLE;
    ATIM_BDTCfgStruct.BreakFilter      = 6;                           // 刹车信号滤波设置
    ATIM_BDTCfgStruct.BreakPolarity    = ATIM_BREAKPOLARITY_HIGH;     // 刹车极性设置
    ATIM_BDTCfgStruct.BreakState       = ATIM_BREAK_ENABLE;           // 刹车使能
    ATIM_BDTCfgStruct.DeadTime         = 6;                           // 死区时间设置
    ATIM_BDTCfgStruct.LockLevel        = ATIM_LOCKLEVEL_OFF;          // 锁定级别配置
    ATIM_BDTCfgStruct.OffStateRunMode  = ATIM_OSSR_ENABLE;            // 运行模式下的关闭状态管脚配置
    ATIM_BDTCfgStruct.OffStateIDLEMode = ATIM_OSSI_ENABLE;            // 空闲模式下的关闭状态选择
    HAL_ATIM_ConfigBreakDeadTime(&hatim, &ATIM_BDTCfgStruct);

    ATIM_BKInCfgStruct.Source   = ATIM_BREAKINPUTSOURCE_BKIN;         // 刹车信号源设置
    ATIM_BKInCfgStruct.Polarity = ATIM_BREAKINPUTSOURCE_POLARITY_LOW; // 刹车管脚BKIN信号极性
    ATIM_BKInCfgStruct.Enable   = ATIM_BREAKINPUTSOURCE_ENABLE;       // 刹车信号源使能
    HAL_ATIM_ConfigBreakInput(&hatim, ATIM_BREAKINPUT_BRK, &ATIM_BKInCfgStruct);

    // 配置软件写COMG触发COMG事件, 开启COMG中断
    HAL_ATIM_ConfigCommutEvent_IT(&hatim, ATIM_TS_NONE, ATIM_COMMUTATION_SOFTWARE);

    // 通道输出使能
    ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_1 | ATIM_CHANNEL_2 | ATIM_CHANNEL_3, ATIM_CCx_ENABLE);
    ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_1 | ATIM_CHANNEL_2 | ATIM_CHANNEL_3, ATIM_CCxN_ENABLE);

    // 使能ATIM
    HAL_ATIM_ENABLE(&hatim);

    // 主输出使能
    HAL_ATIM_MOE_ENABLE(&hatim);
}


/*----------------------------------------------------------------------------
  The following Table  describes the TIM1 Channels states:
             | Step1 | Step2 | Step3 | Step4 | Step5 | Step6 |
   ----------------------------------------------------------
  |Channel1  |  PWM  |   0   |   0   |   0   |   0   |  PWM  |
  |Channel1N |   0   |   0   |   1   |   1   |   0   |   0   |
  |Channel2  |   0   |   0   |   0   |  PWM  |  PWM  |   0   |
  |Channel2N |   1   |   1   |   0   |   0   |   0   |   0   |
  |Channel3  |   0   |  PWM  |  PWM  |   0   |   0   |   0   |
  |Channel3N |   0   |   0   |   0   |   0   |   1   |   1   |
   ----------------------------------------------------------
----------------------------------------------------------------------------*/

/*******************************************************************************
 * @brief ATIM 6步换向处理服务，本样例在SysTick中断服务程序中调用
 * @param  None
 * @retval None
 *
 */
void BLDC_6StepProcess(void)
{
     if (Step == 1)
     {
         /* Next step: Step 2 Configuration ---------------------------- */
         /*  Channel3 configuration */
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCxN_DISABLE);

         /*  Channel1 configuration */
         SYM_ATIM->CCMR1CMP_f.OC1M = OCMODE_PWM1;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCx_ENABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCxN_DISABLE);

         /*  Channel2 configuration */
         SYM_ATIM->CCMR1CMP_f.OC2M = OCMODE_FORCED;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCxN_ENABLE);

         Step++;
     }
     else if (Step == 2)
     {
         /* Next step: Step 3 Configuration ---------------------------- */
         /*  Channel1 configuration */
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCxN_DISABLE);

         /*  Channel3 configuration */
         SYM_ATIM->CCMR2CMP_f.OC3M = OCMODE_PWM1;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCx_ENABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCxN_DISABLE);

         /*  Channel2 configuration */
         SYM_ATIM->CCMR1CMP_f.OC2M = OCMODE_FORCED;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCxN_ENABLE);

         Step++;
     }
     else if (Step == 3)
     {
         /* Next step: Step 4 Configuration ---------------------------- */
         /*  Channel2 configuration */
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCxN_DISABLE);

         /*  Channel3 configuration */
         SYM_ATIM->CCMR2CMP_f.OC3M = OCMODE_PWM1;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCx_ENABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCxN_DISABLE);

         /*  Channel1 configuration */
         SYM_ATIM->CCMR1CMP_f.OC1M = OCMODE_FORCED;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCxN_ENABLE);

         Step++;
     }
     else if (Step == 4)
     {
         /* Next step: Step 5 Configuration ---------------------------- */
         /*  Channel3 configuration */
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCxN_DISABLE);

         /*  Channel2 configuration */
         SYM_ATIM->CCMR1CMP_f.OC2M = OCMODE_PWM1;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCx_ENABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCxN_DISABLE);

         /*  Channel1 configuration */
         SYM_ATIM->CCMR1CMP_f.OC1M = OCMODE_FORCED;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCxN_ENABLE);

         Step++;
     }
     else if (Step == 5)
     {
         /* Next step: Step 6 Configuration ---------------------------- */
         /*  Channel1 configuration */
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCxN_DISABLE);

         /*  Channel2 configuration */
         SYM_ATIM->CCMR1CMP_f.OC2M = OCMODE_PWM1;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCx_ENABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCxN_DISABLE);

         /*  Channel3 configuration */
         SYM_ATIM->CCMR2CMP_f.OC3M = OCMODE_FORCED;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCxN_ENABLE);

         Step++;
     }
     else if (Step == 6)
     {
         /* Next step: Step 1 Configuration ---------------------------- */
         /*  Channel2 configuration */
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_2, ATIM_CCxN_DISABLE);

         /*  Channel1 configuration */
         SYM_ATIM->CCMR1CMP_f.OC1M = OCMODE_PWM1;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCx_ENABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_1, ATIM_CCxN_DISABLE);

         /*  Channel3 configuration */
         SYM_ATIM->CCMR2CMP_f.OC3M = OCMODE_FORCED;
         ATIM_CCxChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCx_DISABLE);
         ATIM_CCxNChannelCmd(hatim.Instance, ATIM_CHANNEL_3, ATIM_CCxN_ENABLE);

         Step=1;
     }
}


/*******************************************************************************
 * @brief  ATIM中断服务程序回调函数
 * @param  None
 *
 * @retval None
 */
void ATIM_IRQHandlerCallBack(void)
{
    if (HAL_ATIM_GET_IT_SOURCE(&hatim, ATIM_IT_SOURCE_COM) &&
        HAL_ATIM_GET_FLAG(&hatim, ATIM_IT_FLAG_COM))
    {
        HAL_ATIM_CLR_FLAG(&hatim, ATIM_IT_FLAG_COM);
    }
}

/*******************************************************************************
 * @brief  SysTick中断服务程序回调函数
 * @param  None
 *
 * @retval None
 */
void SysTick_HandlerCallBack(void)
{
    static uint32_t StepCount = 0;

    if (++StepCount >= 100)
    {
        StepCount = 0;
        BLDC_6StepProcess();
        HAL_ATIM_GenerateEvent(&hatim, ATIM_EVENTSOURCE_COM);         // 软件COMG事件
    }
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
